angular.module('BlocksApp').controller('HomeController', function ($rootScope, $scope, $http, $timeout) {
  $scope.$on('$viewContentLoaded', function () {
    // initialize core components
    App.initAjax();
  });

  var URL = '/data';

  $rootScope.isHome = true;

  $scope.stats = {};
  $scope.reloadStats = function () {
    $http.post("/stats", { "action": "dbstats" })
      .then(function (res) {
        $scope.stats.transactionsDb = Number(res.data.transactionsDb).toLocaleString();
        $scope.stats.blocksDb = Number(res.data.blocksDb).toLocaleString() ;
      });
  }

  $scope.reloadBlocks = function () {
    $scope.blockLoading = true;
    $scope.reloadStats();
    $http({
      method: 'POST',
      url: URL,
      data: { "action": "latest_blocks" }
    }).success(function (data) {
      $scope.blockLoading = false;
      $scope.latest_blocks = data.blocks;
    });
  }


  $scope.reloadStats();
  $scope.reloadBlocks();

  $scope.blockLoading = false;
})
  .directive('summaryStats', function ($http) {
    return {
      restrict: 'E',
      templateUrl: '/views/summary-stats.html',
      scope: true,
      link: function (scope, elem, attrs) {
        scope.stats = {};

        var etcEthURL = "/stats";
        var etcPriceURL = "https://api.coinmarketcap.com/v1/ticker/ethereum-classic/";
        var ethPriceURL = "https://api.coinmarketcap.com/v1/ticker/ethereum/"
        scope.stats.ethDiff = 1;
        scope.stats.ethHashrate = 1;
        scope.stats.usdEth = 1;



        $http.post(etcEthURL, { "action": "etceth" })
          .then(function (res) {
            scope.stats.etcHashrate = res.data.etcHashrate;
            scope.stats.ethHashrate = res.data.ethHashrate;
            scope.stats.etcEthHash = res.data.etcEthHash;
            scope.stats.ethDiff = res.data.ethDiff;
            scope.stats.etcDiff = res.data.etcDiff;
            scope.stats.etcEthDiff = res.data.etcEthDiff;
          });
        $http.get(etcPriceURL)
          .then(function (res) {
            scope.stats.usdEtc = parseFloat(res.data[0]["price_usd"]);
            scope.stats.usdEtcEth = parseInt(100 * scope.stats.usdEtc / scope.stats.usdEth);
          });
        $http.get(ethPriceURL)
          .then(function (res) {
            scope.stats.usdEth = parseFloat(res.data[0]["price_usd"]);
            scope.stats.usdEtcEth = parseInt(100 * scope.stats.usdEtc / scope.stats.usdEth);
            scope.stats.ethChange = parseFloat(res.data.change);
          });

      }
    }
  });
